package ckw.gui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import ckw.database.Configuration;
import ckw.database.DatabaseManager;
import ckw.database.OKW;

public class DlgDbOKWAddEdit extends Dialog {
	private Text txtNazwa;
	private Text txtIdentyfikator;
	private Text txtAdres;
	
	private OKW okw;
	private Configuration configuration;
	private boolean edit;
	private Text txtAdresIp;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public DlgDbOKWAddEdit(Shell parentShell, OKW okw, boolean edit) {
		super(parentShell);
		
		this.okw = okw;
		this.edit = edit;
		if(edit) {
			configuration = DatabaseManager.getInstance().get(Configuration.class, okw.getConfiguration().getId());
		} else {
			configuration = new Configuration();
		}
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(2, false));
		
		Label lblNazwa = new Label(container, SWT.NONE);
		lblNazwa.setText("Nazwa:");
		
		txtNazwa = new Text(container, SWT.BORDER);
		txtNazwa.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		txtNazwa.setText(!edit ? "" : okw.getName());
		
		Label lblIdentyfikator = new Label(container, SWT.NONE);
		lblIdentyfikator.setText("Identyfikator:");
		
		txtIdentyfikator = new Text(container, SWT.BORDER);
		txtIdentyfikator.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		txtIdentyfikator.setText(!edit ? "" : okw.getIdentificationField());
		
		Label lblAdres = new Label(container, SWT.NONE);
		lblAdres.setText("Adres:");
		
		txtAdres = new Text(container, SWT.BORDER);
		txtAdres.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		txtAdres.setText(!edit ? "" : okw.getAddress());
		
		Label label = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gd_label = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
		gd_label.widthHint = 431;
		label.setLayoutData(gd_label);
		
		Label lblAdresIp = new Label(container, SWT.NONE);
		lblAdresIp.setText("Adres IP:");
		
		txtAdresIp = new Text(container, SWT.BORDER);
		txtAdresIp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		txtAdresIp.setText(!edit ? "" : configuration.getIp());
		
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, edit ? "Zapisz" : "Dodaj",
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				"Anuluj", false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
	
	protected void okPressed() {
		okw.setName(txtNazwa.getText());
		okw.setIdentificationField(txtIdentyfikator.getText());
		okw.setAddress(txtAdres.getText());
		
		configuration.setIp(txtAdresIp.getText());
		configuration.setIdentificationField(okw.getIdentificationField());
		
		if(!edit) {
			DatabaseManager.getInstance().save(okw);
			DatabaseManager.getInstance().save(configuration);
			DatabaseManager.getInstance().addConfigurationToOKW(configuration, okw);
		} else {
			DatabaseManager.getInstance().update(okw);
			DatabaseManager.getInstance().update(configuration);
		}
		// TODO: zapis do pliku
		super.okPressed();
	}
}
